import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import HomeView from '../views/admin/HomeView.vue'

const routes: Array<RouteRecordRaw> = [
  {path: '/',redirect: '/login'},
  {
    path: '/login',
    name: 'login',
    component: () => import( '../views/LoginView.vue')
  },
  {
    path: '/admin/home',
    name: 'home',
    component: HomeView,
    children:[
      {
        path:"/admin/user",
        name:"userManagement",
        meta:{
          isShow: true ,
          title:"用户管理"
        },
        component: () => import( '../views/admin/UserView.vue')
      },
      {
        path:"/admin/role",
        name:"roleManagement",
        meta:{
          isShow: true ,
          title:"角色管理"
        },
        component: () => import( '../views/admin/RoleView.vue')
      },
      {
        path:"/admin/permission",
        name:"permissionManagement",
        meta:{
          isShow: false ,
          title:"权限管理"
        },
        component: () => import( '../views/admin/PermissionView.vue')
      },
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
/* 路由守卫 */
router.beforeEach((to,from,next) => {
  const token: string | null = localStorage.getItem('token')
  if(!token && to.path !== '/login'){
    next('/login')
  }else{
    next()
  }
})
export default router
